package com.thirteenClassicsServer.mapper;

import com.thirteenClassics.dto.BookIdAndCatalogDTO;
import com.thirteenClassics.dto.CatalogQueryDTO;
import com.thirteenClassics.dto.CommonDTO;
import com.thirteenClassics.dto.ImportCatalogDTO;
import com.thirteenClassics.entity.Catalog;
import com.thirteenClassics.entity.CatalogBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

@Mapper
public interface CatalogBookMapper {
    Long insert(ImportCatalogDTO importCatalogDTO);

    void insertCatalogBook(ImportCatalogDTO importCatalogDTO);

    Long selectCatalogBookId(CommonDTO commonDTO);

    CatalogBook selectCatalogBookAll(CommonDTO commonDTO);

    CatalogBook selectCatalogBookAllMinority(CommonDTO commonDTO);

    Long selectEntryIdByBookIdAndCatalogId(BookIdAndCatalogDTO bookIdAndCatalogDTO);

    void updatajdContent(CatalogBook catalogBook);

    @Delete("delete from catalog_book where bk_id=#{id}")
    void deleteByBkId(Long id);

    @Select("select id from catalog_book where bk_id=#{id}")
    List<Long> selectEntryIdByBookId(Long id);

    @Select("select id from catalog_minority where bk_id=#{id}")
    List<Long> selectIdByBookId(Long id);

    @Update("update catalog_book set sentence_num=#{sentenceNum} where id=#{entryId}")
    void updateSentenceNum(Long sentenceNum, Long entryId);

    @Update("update catalog_minority set sentence_num=#{sentenceNum} where id=#{entryId}")
    void updateMinoritySentenceNum(Long sentenceNum, Long entryId);

    @Select("select sentence_num from catalog_book where id=#{entryId}")
    Long selectSentenceNumById(Long entryId);

    @Select("select sentence_num from catalog_minority where id=#{entryId}")
    Long selectMinoritySentenceNumById(Long entryId);

    @Select("select catelog_id from catalog_book where id=#{entryId}")
    Long selectCatelogIdById(Long entryId);

    List<Catalog> selectCatalogOrdersByName(CatalogQueryDTO catalogQueryDTO);

    List<Catalog> selectCatalogOrdersByNameAndId(CatalogQueryDTO catalogQueryDTO);

    int updateMinorityBkId(String bookName, String bookLang, Long id);

    @Update("update catalog_minority set bk_id=null where bk_id=#{id}")
    void updateMinorityBkIdTONull(Long id);

    @Select(("select id from catalog_book where catelog_id=#{catalogIds}"))
    int selectEntryIdByCatalogId(int catalogIds);

    @Cacheable(value = "catalogBooks", key = "#id")
    CatalogBook selectCatalogBookAllMinorityById(Long id);
}